var errorPlaceName = true;
var errorPlaceLinkMan = true;
var errorOfficePhone = true;
var errorAddress = true;
var errorMobile = true;
var errorBussinessItem = true;

var businessItems = new Object();

$(document).ready(function(){
	freshPlaceList();
});

function freshPlaceList() {
	var requestUrl = "merchant_json_servlet";
	var placesHtml = "";
	var placesCoopHtml = "";
	var params = {
			request_method:"get_places",
			type:0
	};
	
	$.post(requestUrl, params, function(data){
		if (data.code != 0) {return;}
		
		for (var i = 0; data.resultData && data.resultData.items && i < data.resultData.items.length; i++) {
			var place = data.resultData.items[i];
			
			placesHtml += '<div style="padding-left: 40px; padding-top: 15px;">';
			placesHtml += '	<div style="width: 340px; height: 30px; display: inline-block; background: url(\'../image/bg_place_name.png\') no-repeat;">';
			placesHtml += place.placeName;
			placesHtml += '	</div>';
			placesHtml += '	<img class="pointer fade_effect" src="../image/btn_modify.png" align="top" style="margin-left: 15px;" onclick="modifyPlace(\'' + place.id + '\');"/>';
			placesHtml += '	<img class="pointer fade_effect" src="../image/btn_delete.png" align="top" style="margin-left: 15px;" onclick="deletePlace(\'' + place.id + '\');"/>';
			placesHtml += '</div>';
			placesHtml += '';
			placesHtml += '';
			placesHtml += '';
			placesHtml += '';
			placesHtml += '';
		}
		
		$("#div_place_list").html(placesHtml);
		addFadeEffect();
	}, "json");
	
	params.type = 1;
	$.post(requestUrl, params, function(data){
		if (data.code != 0) {return;}
		
		for (var i = 0; data.resultData && data.resultData.items && i < data.resultData.items.length; i++) {
			var place = data.resultData.items[i];
			
			placesCoopHtml += '<div style="padding-left: 40px; padding-top: 15px;">';
			placesCoopHtml += '	<div style="width: 340px; height: 30px; display: inline-block; background: url(\'../image/bg_place_name.png\') no-repeat;">';
			placesCoopHtml += place.placeName;
			placesCoopHtml += '	</div>';
			placesCoopHtml += '	<img class="pointer fade_effect" src="../image/btn_modify.png" align="top" style="margin-left: 15px;" onclick="modifyPlace(\'' + place.id + '\');"/>';
			placesCoopHtml += '	<img class="pointer fade_effect" src="../image/btn_delete.png" align="top" style="margin-left: 15px;" onclick="deletePlace(\'' + place.id + '\');"/>';
			placesCoopHtml += '</div>';
			placesCoopHtml += '';
			placesCoopHtml += '';
			placesCoopHtml += '';
			placesCoopHtml += '';
			placesCoopHtml += '';
		}
		
		$("#div_place_coop_list").html(placesCoopHtml);
		addFadeEffect();
	}, "json");
}

function toggleAddPlaceDiv() {
	if ($("#div_add_place").css("display") == "none") {
		$("#div_add_place").css("display", "");
	} else {
		$("#div_add_place").css("display", "none");
	}
}

function toggleAddPlaceCoopDiv() {
	if ($("#div_add_place_coop").css("display") == "none") {
		freshAvailablePlaces();
		$("#div_add_place_coop").css("display", "");
	} else {
		$("#div_add_place_coop").css("display", "none");
	}
}

function addMerchantPlace() {
	if (checkPlaceName() && !errorPlaceName
			&& checkPlaceLinkMan() && !errorPlaceLinkMan
			&& checkOfficePhone() && !errorOfficePhone
			&& checkPlaceAddress() && !errorAddress
			&& checkMobile() && !errorMobile
			&& checkBusinessItem() && !errorBussinessItem) {
		var requestUrl = "merchant_json_servlet";
		var params = {
				request_method:"add_place",
				place_name:$("#place_name").val(),
				place_linkman:$("#place_linkman").val(),
				office_phone:$("#office_phone").val(),
				address:$("#address").val(),
				road_nearby:$("#road_nearby").val(),
				mobile:$("#mobile").val(),
				business_item:getBusinessItemStr()
		};
		
		$.post(requestUrl, params, function(ret) {
			if (ret.code == 0) {
				toggleAddPlaceDiv();
				freshPlaceList();
			}
		}, "json");
	}
}

function addMerchantPlaceCoop() {
	if ($("#slt_place_id").val() != "") {
		var requestUrl = "merchant_json_servlet";
		var params = {
				request_method:"add_place_coop",
				place_ids:$("#slt_place_id").val()
		};
		
		$.post(requestUrl, params, function(ret) {
			if (ret.code == 0) {
				toggleAddPlaceCoopDiv();
				freshPlaceList();
			}
		}, "json");
	}
}

function freshImage(data) {
	var result = eval('(' + data + ')');
	var imageName = "";

	if (result.code == "0") {
		imageName = result.resultData;
		document.getElementById("img_place").src = WSContextPath + "/image/uploads/" + imageName;
	} else {
		showMessage(result.message);
	}
	document.getElementById("image_name").value = imageName;
}

function checkPlaceName() {
	if ($("#place_name").val() == "") {
		$("#error_message_add_place").html("场所名称错误");
		$("#place_name").css("background", "#fbe2e2");
		errorPlaceName = true;
	} else {
		$("#error_message_add_place").html("");
		$("#place_name").css("background", "#ffffff");
		errorPlaceName = false;
	}
	
	return true;
}

function checkPlaceLinkMan() {
	if ($("#place_linkman").val() == "") {
		$("#error_message_add_place").html("场所联系人错误");
		$("#place_linkman").css("background", "#fbe2e2");
		errorPlaceLinkMan = true;
	} else {
		$("#error_message_add_place").html("");
		$("#place_linkman").css("background", "#ffffff");
		errorPlaceLinkMan = false;
	}
	
	return true;
}

function checkOfficePhone() {
	var pattern = /^[0-9]{8}/;
	
	if ($("#office_phone").val() == "" || !pattern.test($("#office_phone").val())) {
		$("#error_message").html("固定电话错误<br/>");
		$("#office_phone").css("background", "#fbe2e2");
		errorOfficePhone = true;
	} else {
		$("#error_message").html("");
		$("#office_phone").css("background", "#ffffff");
		errorOfficePhone = false;
	}
	return true;
}

function checkPlaceAddress() {
	if ($("#address").val() == "") {
		$("#error_message_add_place").html("场所地址错误");
		$("#address").css("background", "#fbe2e2");
		errorAddress = true;
	} else {
		$("#error_message_add_place").html("");
		$("#address").css("background", "#ffffff");
		errorAddress = false;
	}
	
	return true;
}

function checkMobile() {
	var partern = /^[0-9]{11}$/;
	
	if ($("#mobile").val() != "" && !partern.test($("#mobile").val())) {
		$("#error_message_add_place").html("手机号错误");
		$("#mobile").css("background", "#fbe2e2");
		errorMobile = true;
	} else {
		$("#error_message_add_place").html("");
		$("#mobile").css("background", "#ffffff");
		errorMobile = false;
	}
	
	return true;
}

function checkBusinessItem() {
	for (var key in businessItems) {
		if (businessItems[key] == true) {
			$("#error_message_add_place").html("");
			errorBussinessItem = false;
			return true;
		}
	}
	
	errorBussinessItem = true;
	$("#error_message_add_place").html("请选择营业项目");
	return false;
}

function toggleBusinessItem(itemId) {
	if (businessItems[itemId] == true) {
		businessItems[itemId] = false;
		$("#item_" + itemId).css("background", "url('../image/radio_gray_unselected.png') center no-repeat");
	} else {
		businessItems[itemId] = true;
		$("#item_" + itemId).css("background", "url('../image/radio_gray_selected.png') center no-repeat");
	}
}

function getBusinessItemStr() {
	var ret = "";
	
	for (var item in businessItems) {
		if (businessItems[item] == true) {
			ret += (ret == "") ? "" : ",";
			ret += item;
		}
	}
	
	return ret;
}

function freshAvailablePlaces() {
	var requestUrl = "merchant_json_servlet";
	var params = {
			request_method:"get_available_places"
	};
	
	var availablePlaceHtml = "";
	$.post(requestUrl, params, function(data) {
		availablePlaceHtml += '<option value="">请选择</option>';
		for (var i = 0; data.resultData && i < data.resultData.length; i++) {
			var place = data.resultData[i];
			
			availablePlaceHtml += '<option value="' + place.id + '">' + place.placeName + '</option>';
		}
		
		$("#slt_place_id").html(availablePlaceHtml);
	}, "json");
}